<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Table extends CI_Table {

	var $horizontal 		= false;
	var $filter				= array();

	function _default_template()
	{
		return  array (
						'table_open' 			=> '<table class="table" border="1">',

						'heading_row_start' 	=> '<thead><tr>',
						'heading_row_end' 		=> '</tr></thead><tbody>',
						'heading_cell_start'	=> '<th valign="bottom">',
						'heading_cell_end'		=> '</th>',
						
						'subheading_cell_start'	=> '<th valign="bottom" class="sth">',
						'subheading_cell_end'	=> '</th>',						

						'row_start' 			=> '<tr>',
						'row_end' 				=> '</tr>',
						'cell_start'			=> '<td valign="top">',
						'cell_end'				=> '</td>',						
						'cell_heading_start'	=> '<td class="th" valign="top">',
						'cell_heading_end'		=> '</td>',						

						'row_alt_start' 		=> '<tr class="alternate">',
						'row_alt_end' 			=> '</tr>',
						'cell_alt_start'		=> '<td valign="top">',
						'cell_alt_end'			=> '</td>',
						'cell_alt_heading_start'=> '<td class="th_alternate" valign="top">',
						'cell_alt_heading_end'	=> '</td>',

						'table_close' 			=> '</tbody></table>'
					);
	}
	
 	function _compile_template()
 	{ 	
 		if ($this->template == NULL)
 		{
 			$this->template = $this->_default_template();
 			return;
 		}
		
		$this->temp = $this->_default_template();
		foreach (array('table_open','heading_row_start', 'heading_row_end', 'heading_cell_start', 'heading_cell_end', 'subheading_cell_start', 'subheading_cell_end', 'row_start', 'row_end', 'cell_start', 'cell_end', 'row_alt_start', 'row_alt_end', 'cell_alt_start', 'cell_alt_end', 'table_close') as $val)
		{
			if ( ! isset($this->template[$val]))
			{
				$this->template[$val] = $this->temp[$val];
			}
		} 	
 	}	
	
	function set_template($template, $based_on_default = false)
	{
		if ( ! is_array($template))
		{
			return FALSE;
		}
	
		if ($based_on_default) {
			$this->template = $this->_default_template();
		}
	
		foreach($template as $key => $val) {
			$this->template[$key] = $val;
		}
	}
	
	function set_horizontal($status)
	{
		$this->horizontal = $status;
	}
	
	function is_horizontal()
	{
		return $this->horizontal;
	}
	
	function set_filter()
	{
		$args = func_get_args();
		$this->filter = (is_array($args[0])) ? $args[0] : $args;
	}
	
	/*function _filter($heading, $maxlen) { //XXX, ambil dari data filter
		$filter = '';
		
		if (strlen(trim(strip_tags($heading)))>0) {
			$filter = '<input name="filter" size="'.$maxlen.'" onkeyup="Table.filter(this,this)">';
		}
		
		return $filter;
	}*/

	function generate($table_data = NULL) {
		if (!$this->is_horizontal()) {
			// The table data can optionally be passed to this function
			// either as a database result object or an array
			if ( ! is_null($table_data))
			{
				if (is_object($table_data))
				{
					$this->_set_from_object($table_data);
				}
				elseif (is_array($table_data))
				{
					$set_heading = (count($this->heading) == 0 AND $this->auto_heading == FALSE) ? FALSE : TRUE;
					$this->_set_from_array($table_data, $set_heading);
				}
			}
		
			// Is there anything to display?  No?  Smite them!
			if (count($this->heading) == 0 AND count($this->rows) == 0)
			{
				return 'Undefined table data';
			}
		
			// Compile and validate the template date
			$this->_compile_template();
		
		
			// Build the table!
			
			$out = $this->template['table_open'];
			$out .= $this->newline;		

			// Add any caption here
			if ($this->caption)
			{
				$out .= $this->newline;
				$out .= '<caption>' . $this->caption . '</caption>';
				$out .= $this->newline;
			}

			// Is there a table heading to display?
			if ((count($this->heading) > 0) || (count($this->filter) > 0))
			{
				$out .= $this->template['heading_row_start'];
				$out .= $this->newline;		

				if (count($this->heading) > 0)  {
					foreach($this->heading as $heading)
					{
						$heading_cell_start = $this->template['heading_cell_start'];
						if (strlen(trim($heading))<=0) {
							$heading_cell_start = str_replace('table-sortable:alphanumeric', '', $heading_cell_start);
						}
						$out .= $heading_cell_start;
						$out .= $heading;
						$out .= $this->template['heading_cell_end'];
					}

					$out .= $this->template['row_end'];
					$out .= $this->newline;
				}
				
				if (count($this->filter) > 0)  {
					$out .= $this->template['row_start'];
					$out .= $this->newline;
					
					$out .= '<form name="filter_form">';
					foreach($this->filter as $filter)
					{					
						$out .= $this->template['subheading_cell_start'];
						$out .= $filter;
						$out .= $this->template['subheading_cell_end'];
					}
					$out .= '</form>';
				}
				
				/*//XXX:
				$out .= $this->template['row_start'];
				$out .= $this->newline;
			
				$i = 1;
				foreach($this->heading as $heading)
				{
					$j = 1;
					$idx = "";
					$maxlen = 0;
					foreach($this->rows as $row)
					{
						if ( ! is_array($row))
						{
							break;
						}
			
						if ($j==1) {
							// find cell idx
							$k = 1;						
							foreach($row as $cell_idx => $cell)
							{
								if ($k==$i) {
									$idx = $cell_idx;
									break;
								}
								$k++;
							}
						}
						
						$stripped_cell = trim(strip_tags(html_entity_decode(str_replace("&nbsp;", "", $row[$idx]))));
						//echo $stripped_cell;
						if (strlen($stripped_cell)>$maxlen)
							$maxlen = strlen($stripped_cell);
						
						$j++;
					}

					if ($this->filter) {					
						$out .= $this->template['subheading_cell_start'];
						//$out .= $maxlen;
						$out .= $this->_filter($heading, $maxlen);
						$out .= $this->template['subheading_cell_end'];
					}
					$i++;
				}
				*/
				$out .= $this->template['heading_row_end'];
				$out .= $this->newline;
			}

			// Build the table rows
			if (count($this->rows) > 0)
			{
				$i = 1;
				foreach($this->rows as $row)
				{
					if ( ! is_array($row))
					{
						break;
					}
				
					// We use modulus to alternate the row colors
					$name = (fmod($i++, 2)) ? '' : 'alt_';
				
					$out .= $this->template['row_'.$name.'start'];
					$out .= $this->newline;		
		
					foreach($row as $cell)
					{
						$out .= $this->template['cell_'.$name.'start'];
						
						if ($cell === "")
						{
							$out .= $this->empty_cells;
						}
						else
						{
							$out .= $cell;
						}
						
						$out .= $this->template['cell_'.$name.'end'];
					}
		
					$out .= $this->template['row_'.$name.'end'];
					$out .= $this->newline;	
				}
			}

			$out .= $this->template['table_close'];
		
			return $out;
		} else {	
			// XXX: untested
			// The table data can optionally be passed to this function
			// either as a database result object or an array
			if ( ! is_null($table_data))
			{
				if (is_object($table_data))
				{
					$this->_set_from_object($table_data);
				}
				elseif (is_array($table_data))
				{
					$this->_set_from_array($table_data, FALSE); // Always disable heading
				}
			}
		
			$this->auto_heading = FALSE;
							
			// Is there anything to display?  No?  Smite them!
			if (count($this->rows) == 0)
			{
				return 'Undefined table data';
			}
		
			// Compile and validate the template date
			$this->_compile_template();

			$this->template['table_open'] = '<table class="table">';
		
			// Build the table!
			
			$out = $this->template['table_open'];
			$out .= $this->newline;		

			// Add any caption here
			if ($this->caption)
			{
				$out .= $this->newline;
				$out .= '<caption>' . $this->caption . '</caption>';
				$out .= $this->newline;
			}

			// Is there a table heading to display?
			if (count($this->heading) > 0)
			{
				$out .= $this->template['heading_row_start'];
				$out .= $this->newline;		

				foreach($this->heading as $heading)
				{
					$out .= $this->template['heading_cell_start'];
					$out .= $heading;
					$out .= $this->template['heading_cell_end'];
				}

				$out .= $this->template['heading_row_end'];
				$out .= $this->newline;				
			}			
			
			// Build the table rows
			if (count($this->rows) > 0)
			{
				$i = 1;
				foreach($this->rows as $row)
				{
					if ( ! is_array($row))
					{
						break;
					}
				
					// We use modulus to alternate the row colors
					$name = (fmod($i++, 2)) ? '' : 'alt_';
				
					$out .= $this->template['row_'.$name.'start'];
					$out .= $this->newline;		
		
					$j = 1;					
					foreach($row as $cell)
					{
						if ($j==1) {
							$out .= $this->template['cell_'.$name.'heading_start'];
						} else {
							$out .= $this->template['cell_'.$name.'start'];
						}
						
						if ($cell === "")
						{
							$out .= $this->empty_cells;
						}
						else
						{
							$out .= $cell;
						}
						
						if ($j==1) {
							$out .= $this->template['cell_'.$name.'heading_end'];
						} else {
							$out .= $this->template['cell_'.$name.'end'];
						}
						$j++;
					}
		
					$out .= $this->template['row_'.$name.'end'];
					$out .= $this->newline;	
				}
			}

			$out .= $this->template['table_close'];
		
			return $out;
		}
	}
	
}


/* End of file MY_Table.php */
/* Location: ./system/application/libraries/MY_Table.php */